package com.backend.apartment.service.impl;

import java.util.List;
import com.backend.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.backend.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.backend.apartment.domain.ApartmentType;
import com.backend.apartment.mapper.ApartmentMapper;
import com.backend.apartment.domain.Apartment;
import com.backend.apartment.service.IApartmentService;

/**
 * 房间管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class ApartmentServiceImpl implements IApartmentService
{
    @Autowired
    private ApartmentMapper apartmentMapper;

    /**
     * 查询房间管理
     *
     * @param apartmentId 房间管理主键
     * @return 房间管理
     */
    @Override
    public Apartment selectApartmentByApartmentId(Long apartmentId)
    {
        return apartmentMapper.selectApartmentByApartmentId(apartmentId);
    }

    /**
     * 查询房间管理列表
     *
     * @param apartment 房间管理
     * @return 房间管理
     */
    @Override
    public List<Apartment> selectApartmentList(Apartment apartment)
    {
        return apartmentMapper.selectApartmentList(apartment);
    }

    /**
     * 新增房间管理
     *
     * @param apartment 房间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertApartment(Apartment apartment)
    {
        apartment.setCreateTime(DateUtils.getNowDate());
        int rows = apartmentMapper.insertApartment(apartment);
        insertApartmentType(apartment);
        return rows;
    }

    /**
     * 修改房间管理
     *
     * @param apartment 房间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateApartment(Apartment apartment)
    {
        apartment.setUpdateTime(DateUtils.getNowDate());
//        apartmentMapper.deleteApartmentTypeByApartmentId(apartment.getApartmentId());
        insertApartmentType(apartment);
        return apartmentMapper.updateApartment(apartment);
    }

    /**
     * 批量删除房间管理
     *
     * @param apartmentIds 需要删除的房间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteApartmentByApartmentIds(Long[] apartmentIds)
    {
        apartmentMapper.deleteApartmentTypeByApartmentIds(apartmentIds);
        return apartmentMapper.deleteApartmentByApartmentIds(apartmentIds);
    }

    /**
     * 删除房间管理信息
     *
     * @param apartmentId 房间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteApartmentByApartmentId(Long apartmentId)
    {
        apartmentMapper.deleteApartmentTypeByApartmentId(apartmentId);
        return apartmentMapper.deleteApartmentByApartmentId(apartmentId);
    }

    /**
     * 新增房间类型信息
     *
     * @param apartment 房间管理对象
     */
    public void insertApartmentType(Apartment apartment)
    {
        List<ApartmentType> apartmentTypeList = apartment.getApartmentTypeList();
        Long apartmentId = apartment.getApartmentId();
        if (StringUtils.isNotNull(apartmentTypeList))
        {
            List<ApartmentType> list = new ArrayList<ApartmentType>();
            for (ApartmentType apartmentType : apartmentTypeList)
            {
                if(apartmentType.getApartmentTypeId() != null){
                    apartmentMapper.updateApartmentType(apartmentType);
                }else{
                    apartmentType.setApartmentId(apartmentId);
                    list.add(apartmentType);
                }
            }
            if (list.size() > 0)
            {
                apartmentMapper.batchApartmentType(list);
            }
        }
    }
}
